<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Upgrade_premium extends CI_Controller {

	function index() {
		show_404(base_url());		
	}
	
	public function idea() {
		$this->load-> model('idea_model');
		$ideaId = $this->input->post('idea_id');
		$data['idea'] = $this->idea_model->getIdeaById($ideaId);
		$this -> load -> view('upgrade_premium', $data);
		
	}
/*
 * This function received the POST information sent by PayPal after the transaction completed, and process accordingly 
 * to the database
 */
	public function ipn_handler()
	{
		$req = 'cmd=' . urlencode('_notify-validate');
 
		foreach ($_POST as $key => $value) {
			$value = urlencode(stripslashes($value));
			$req .= "&$key=$value";
		}
 
 
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'https://www.paypal.com/cgi-bin/webscr');
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: www.paypal.com'));
		$res = curl_exec($ch);
		curl_close($ch);
		 
		 
		// assign posted variables to local variables
		$item_name = $_POST['item_name'];
		$item_number = $_POST['item_number'];
		$payment_status = $_POST['payment_status'];
		$payment_amount = $_POST['mc_gross'];
		$payment_currency = $_POST['mc_currency'];
		$txn_id = $_POST['txn_id'];
		$receiver_email = $_POST['receiver_email'];
		$payer_email = $_POST['payer_email'];
		$item_option = $_POST['option_selection1'];
		$first_name = $_POST['first_name'];
		$last_name = $_POST['last_name'];
		$payment_date = $_POST['payment_date'];
		$mc_gross = $_POST['mc_gross'];
		$receiver_id = $_POST['receiver_id'];
		 
		if (strcmp ($res, "VERIFIED") == 0) {
			// check the payment_status is Completed
			// check that txn_id has not been previously processed
			// check that receiver_email is your Primary PayPal email
			// check that payment_amount/payment_currency are correct
			// process payment
			
			
		  $this->load->model('idea_model');
			
			//parse the item name into idea id
			$tempArr = explode(" ", $item_name);
				$idea_id = (integer)$tempArr[4];
			
			//parse the option into duration
			$tmpArr = explode(" ", $item_option);
			
			$this->load->helper('date');
			$advanceDays = time()+(86400*((integer)$tmpArr[0]));//count the duration based on php date
			$expiredDate = date("Y-m-d H:i:s", $advanceDays);
			
			
			
			$success = $this->idea_model->upgradePremium($idea_id, $expiredDate);//call the model, send the data to be insterted to the db
			
			if($success == 1){//if query execution succeeded
				$this -> load -> library('email');
		
					$config['protocol'] = 'mail';
					$config['wordwrap'] = TRUE;
					$config['smtp_timeout'] = '7';
					$config['smtp_host'] = 'ssl://smtp.gmail.com';
					$config['smtp_port'] = 465;
					$config['smtp_user'] = 'ideascover@gmail.com';
					$config['smtp_pass'] = 'airlangga';
					$config['charset'] = 'utf-8';
					$config['mailtype'] = 'text';
					$config['newline'] = "\r\n";
					//$config['smtp_host'] = 'smtp.gmail.com';
					$this -> email -> initialize($config);
		
					$this -> email -> from('ideascover@gmail.com', 'IDeascover Admin');
					$this -> email -> to($payer_email);
					$this -> email -> subject("$item_name");
					$this -> email -> message("
This is an auto-generated email, please do not reply to this notification
=======================================================
					
Dear $first_name $last_name,
					
Your have sucessfully purchased the following item for $$mc_gross:
					
		$item_name
				
		with duration of:$item_option
				
					
on $payment_date with transaction ID: $receiver_id
					
Your premium idea will expired on $expiredDate 
		
Please note upon the expiry date is passed the status of your idea will return back to REGULAR idea.
					
					
					
Thank You for choosing Ideascover!", 'Upgrade To Premium');
					$this -> email -> send();
			}
			
		}
		else if (strcmp ($res, "INVALID") == 0) {
			// log for manual investigation
		}
	}
	

}